package cn.wyl.org.controller;

import cn.wyl.basic.util.AxiousResult;
import cn.wyl.basic.util.PageList;
import cn.wyl.org.domain.Department;
import cn.wyl.org.query.DepartmentQuery;
import cn.wyl.org.service.DepartmentService;
import cn.wyl.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
/*swagger:打在Controller对Controller做描述*/
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private DepartmentService service;
    /**
     * 查询单个对象
     * 请求地址：  http://localhost/department/1
     * @param id
     * @return
     */
    /*等价于：@RequestMapping( value=?,method = {RequestMethod.GET}   )*/
    @GetMapping("/{id}")
    /*swagger:打在方法上，对方法做描述*/
    @ApiOperation(value = "通过ID查询" )
    /*swagger:打在方法上，对方法参数做描述约定.paramType = "path"：从url路径中解析参数id,dataType = "Long"，参数类型为Long*/
    @ApiImplicitParam(name="id",value = "部门id",required = true,paramType = "path",dataType = "Long")
    @PreAuthorize(name = "查询部门",sn = "department:loadById")
    public Department loadById(
            /*swagger:打在参数前，对参数做描述*/
            //@ApiParam(name="编号",required = true)
            @PathVariable("id") Long id)
    {
        return service.loadById(id);
    }

    /**
     * 查询所有对象
     *请求地址：  http://localhost/department
     * @return
     */
    @GetMapping
    @PreAuthorize(name = "获取所有部门",sn = "department:loadAll")
    public List<Department> loadAll() {
        return service.loadAll();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    /*等价于：@RequestMapping( value=?, method = {RequestMethod.DELETE}   )
    * @PathVariable从路径中获取变量
    * 请求参数携带在地址栏里默认get请求
    * */
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public AxiousResult delete(@PathVariable("id") Long id) {
        try {
            if (service.loadById(id)!=null) {
                service.remove(id);
                return new AxiousResult();
            }
            else {
                return new AxiousResult(false, "删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false, "删除失败");
        }

    }

    /**
     * 添加或修改
     * @param department
     * @return
     */
    @PutMapping
    /*@RequestBody从请求体【请求数据包中】中获取数据 默认post请求
    * */
    @PreAuthorize(name = "添加或更新部门",sn = "department:addOrUpdate")
    public AxiousResult addOrUpdate(@RequestBody Department department){
        try{
            if (department.getId()==null){
            service.save(department);
            }else{
               if (service.loadById(department.getId())!=null){
                   service.update(department);
               }else{
                   return new AxiousResult(false, "操作失败");
               }

            }
            return new AxiousResult();
        } catch (Exception e){
            e.printStackTrace();
            return new AxiousResult(false, "操作失败");
        }

    }

    /**
     * 分页查询或高级查询  请求地址：  http://localhost/department
     * @param
     * @return
     */
    @PostMapping
    /*@RequestBody从请求体【请求数据包中】中获取数据 默认post请求
     * */
    @PreAuthorize(name = "分页高级查询部门",sn = "department:queryPage")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){

        return service.queryPage(query);
    }

    // 前端请求体传过来[1,3]
    @PatchMapping
    @PreAuthorize(name = "批量删除部门",sn = "department:patchDel") //value后面用来做权限校验
    public AxiousResult patchDel(@RequestBody List<Long> ids) {
        try {
            System.out.println(ids);
            service.patchDel(ids);
            return new AxiousResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AxiousResult(false, "删除失败");
        }

    }

    //DepartmentController
    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    @PreAuthorize(name = "获取部门树",sn = "department:deptTree") //value后面用来做权限校验
    public List<Department> deptTree(){
        return service.deptTree();
    }


}
